package com.example.db2dm.config;

import com.example.db2dm.util.DatabaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.sql.Connection;
import java.sql.SQLException;

@Configuration
public class DatabaseConfig {
    
    @Autowired
    private MigrationProperties migrationProperties;
    
    /*
     * 创建源数据库连接
     */
    public Connection createSourceConnection() throws SQLException {
        MigrationProperties.DatabaseConfig sourceConfig = migrationProperties.getSource();
        String url = getUrl(sourceConfig);
        return DatabaseUtil.getConnection(url, sourceConfig.getUsername(), sourceConfig.getPassword());
    }
    
    /*
     * 创建目标数据库连接
     */
    public Connection createTargetConnection() throws SQLException {
        MigrationProperties.DatabaseConfig targetConfig = migrationProperties.getTarget();
        String url = getUrl(targetConfig);
        return DatabaseUtil.getConnection(url, targetConfig.getUsername(), targetConfig.getPassword());
    }
    
    /*
     * 获取数据库连接URL
     * 如果配置了URL则直接使用，否则根据其他配置参数构建URL
     */
    private String getUrl(MigrationProperties.DatabaseConfig config) {
        // 如果直接配置了URL，则优先使用
        if (config.getUrl() != null && !config.getUrl().isEmpty()) {
            return config.getUrl();
        }
        
        // 否则根据其他配置参数构建URL
        String host = config.getHost();
        int port = config.getPort();
        String database = config.getDatabase();
        String serviceName = config.getServiceName();
        String type = config.getType();
        
        if (host == null || host.isEmpty() || port <= 0 || type == null || type.isEmpty()) {
            throw new IllegalArgumentException("数据库配置不完整，请提供完整的URL或主机/端口/类型信息");
        }
        
        switch (type.toLowerCase()) {
            case "oracle":
                // 如果提供了service_name，则使用service_name格式
                if (serviceName != null && !serviceName.isEmpty()) {
                    return "jdbc:oracle:thin:@" + host + ":" + port + "/" + serviceName;
                }
                // 否则使用传统的SID格式
                if (database != null && !database.isEmpty()) {
                    return "jdbc:oracle:thin:@" + host + ":" + port + ":" + database;
                }
                throw new IllegalArgumentException("Oracle数据库需要提供database或service-name配置");
                
            case "mysql":
                if (database != null && !database.isEmpty()) {
                    return "jdbc:mysql://" + host + ":" + port + "/" + database + "?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
                }
                throw new IllegalArgumentException("MySQL数据库需要提供database配置");
                
            case "dm":
                if (database != null && !database.isEmpty()) {
                    return "jdbc:dm://" + host + ":" + port + "/" + database + "?charset=UTF-8";
                }
                throw new IllegalArgumentException("达梦数据库需要提供database配置");
                
            default:
                throw new IllegalArgumentException("不支持的数据库类型: " + type);
        }
    }
}